<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" >
      <el-form-item label="设备ID" prop="assetId" label-width="200px">
        <el-input
          v-model="queryParams.assetId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 250px; gap: 250px "
        />
      </el-form-item>
      <el-form-item label="申请人ID" prop="userId" label-width="200px">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入申请人ID"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 250px; gap: 250px "
        />
      </el-form-item>
      <el-form-item label="审核人ID" prop="reviewerId" label-width="200px">
        <el-input
          v-model="queryParams.reviewerId"
          placeholder="请输入审核人ID"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 250px; gap: 250px "
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['assets:scrapLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scrapLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日志ID" align="center" prop="logId" />
      <el-table-column label="设备ID" align="center" prop="assetId" width="180"/>
      <el-table-column label="申请人ID" align="center" prop="userId" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="scrapReason" />
      <el-table-column label="审核人ID" align="center" prop="reviewerId" />
      <el-table-column label="审核时间" align="center" prop="reviewTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="reviewRemark" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag
            :type="getStatusType(scope.row.status)"
            :style="{ backgroundColor: getStatusType(scope.row.status), color: 'white' }"
          >
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报废日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScrapLog, getScrapLog, delScrapLog, addScrapLog, updateScrapLog } from "@/api/assets/scrapLog";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "ScrapLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报废日志表格数据
      scrapLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetId: null,
        userId: null,
        reviewerId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "申请人ID不能为空", trigger: "blur" }
        ],
        applyTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询报废日志列表 */
    getList() {
      this.loading = true;
      listScrapLog(this.queryParams).then(response => {
        this.scrapLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logId: null,
        assetId: null,
        userId: null,
        applyTime: null,
        scrapReason: null,
        reviewerId: null,
        reviewTime: null,
        reviewRemark: null,
        status: null
      };
      this.resetForm("form");
    },
    getStatusText(status) {
      switch (status) {
        case 0:
          return '待审核';
        case 1:
          return '通过';
        case 2:
          return '拒绝';
        default:
          return '';
      }
    },
    getStatusType(status) {
      switch (status) {
        case 1:
          return 'green'; // 通过状态使用绿色标签
        case 2:
          return 'red'; // 拒绝状态使用红色标签
        case 0:
          return '#FFD700'; // 待审核状态使用黄色标签
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报废日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const logId = row.logId || this.ids
      getScrapLog(logId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报废日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.logId != null) {
            updateScrapLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScrapLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const logIds = row.logId || this.ids;
      this.$modal.confirm('是否确认删除报废日志编号为"' + logIds + '"的数据项？').then(function() {
        return delScrapLog(logIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('assets/scrapLog/export', {
        ...this.queryParams
      }, `scrapLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
